package com.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.entity.Customer;
import com.entity.Pagination;
import com.service.CustomerService;


// @Result(type = "json", name = "add")
public class CustomerAction extends BaseAction {
	@Autowired
	private CustomerService customerService;
	private Customer customer;
	private List<Customer> customers;
	private Pagination page;

	private int pageNum = 1;// dwz分页使用
	private int numPerPage = 20;
	public String getStatusCode() {
		return statusCode;
	}

	public void setStatusCode(String statusCode) {
		this.statusCode = statusCode;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getNavTabId() {
		return navTabId;
	}

	public void setNavTabId(String navTabId) {
		this.navTabId = navTabId;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getCallbackType() {
		return callbackType;
	}

	public void setCallbackType(String callbackType) {
		this.callbackType = callbackType;
	}

	public String getForwardUrl() {
		return forwardUrl;
	}

	public void setForwardUrl(String forwardUrl) {
		this.forwardUrl = forwardUrl;
	}

	public String getConfirmMsg() {
		return confirmMsg;
	}

	public void setConfirmMsg(String confirmMsg) {
		this.confirmMsg = confirmMsg;
	}
	public Pagination getPage() {
		return page;
	}

	public void setPage(Pagination page) {
		this.page = page;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}

	public int getNumPerPage() {
		return numPerPage;
	}

	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage;
	}

	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	@Action(value = "show-cus", results = { @Result(name = "success", location = "show-cus.jsp") })
	public String getAllCustomers() {
		page = new Pagination(pageNum, numPerPage);
		int totalCount = customerService.countAll(customer);
		page.setCount(totalCount);
		customers = customerService.getAllCustomers(page, customer);
		return "success";
	}

	@Action(value = "del-cus", results = { @Result(type = "json", name = "success") })
	public String delCus() throws Exception {
		customerService.deleteCustomer(customer);
		setOkJson();
		setForwardUrl("show-cus.action");
		return "success";
	}
	@Action(value = "add-cus", results = { @Result(type = "json", name = "success") })
	public String addCus() throws Exception {
		customerService.insertCustomer(customer);
		setOkJson();
		setForwardUrl("show-cus.action");
		return "success";
	}
	@Action(value = "update-cus", results = { @Result(type = "json", name = "success") })
	public String updateCus() throws Exception {
		System.out.println(customer);
		customerService.updateCustomer(customer);
		setOkJson();
		setForwardUrl("show-cus.action");
		return "success";
	}
	@Action(value = "find-cus", results = { @Result(name = "success", location = "update-cus.jsp")})
	public String findCusById() throws Exception {
		customer = customerService.findById(customer.getId());
		return "success";
	}
}
